package io.jee.caisan.weixin.controller.api;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.jee.alaska.firewall.FirewallService;
import io.jee.alaska.util.IpUtils;
import io.jee.caisan.member.client.model.SendCodeResult;
import io.jee.caisan.member.client.service.RemoteConfirmCodeService;
import io.jee.caisan.member.client.service.RemoteMemberService;

@Controller
@RequestMapping("/api/confirm/code")
public class ConfirmCodeController {
	
	@Resource
	private RemoteConfirmCodeService remoteConfirmCodeService;
	@Resource
	private RemoteMemberService remoteMemberService;
	@Resource
	private FirewallService firewallService;
	
	@RequestMapping("send/register")
	public @ResponseBody SendCodeResult sendRegisterByMobile(String username, String captcha, HttpServletRequest request){
		SendCodeResult result = new SendCodeResult();
		if(KaptchaController.validateCaptcha(captcha, request.getSession())){
			String ip = IpUtils.getIpAddr(request);
			result = remoteConfirmCodeService.sendRegisterCode(username, ip);
		}else{
			result.setErrorField("captcha", "图形验证码错误");
		}
		return result;
	}
	
	@RequestMapping("send/forgot/password")
	public @ResponseBody SendCodeResult sendForgotPassword(String username, String captcha, HttpServletRequest request){
		SendCodeResult result = new SendCodeResult();
		if(KaptchaController.validateCaptcha(captcha, request.getSession())){
			String ip = IpUtils.getIpAddr(request);
			result = remoteConfirmCodeService.sendForgotPasswordCode(username, ip);
		}else{
			result.setErrorField("captcha", "图形验证码错误");
		}
		return result;
	}

}
